package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.Feedback;
import cn.exrick.xboot.modules.transport.vo.FeedbackVo;
import cn.exrick.xboot.modules.transport.param.QueryFeedbackParam;
import cn.exrick.xboot.modules.transport.param.EditFeedbackParam;
import cn.exrick.xboot.modules.transport.service.FeedbackService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "反馈信息-包含订单管理接口")
@RequestMapping("/xboot/feedback")
@Transactional
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<FeedbackVo> get(@PathVariable Long id) {
        return new ResultUtil<FeedbackVo>().setData(feedbackService.getFeedbackById(id));
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<FeedbackVo> saveOrUpdate(@RequestBody EditFeedbackParam editFeedbackParam) {
        //如果为新增，则获取当前登录人id
        if(editFeedbackParam.getId() == null){
            if(editFeedbackParam.getUserId() == null){
                editFeedbackParam.setUserId(securityUtil.getCurrUser().getId());
            }
        }
        if (feedbackService.saveOrUpdate(BeanConverterUtil.convertBean(editFeedbackParam, Feedback.class))) {
            return new ResultUtil<FeedbackVo>().setData(BeanConverterUtil.convertBean(editFeedbackParam, FeedbackVo.class));
        }
        return new ResultUtil<FeedbackVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            feedbackService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<FeedbackVo>> getByCondition(QueryFeedbackParam param,
                                                    PageVo page){

        return new ResultUtil<IPage<FeedbackVo>>().setData(feedbackService.getByCondition(PageUtil.initMpPage(page), param));
    }
}
